<?php


function get_profile($user_id)
{
	global $user;
	
	$info = array();
	$infos = array();
	$sql = "SELECT user_name,birthday,sex,question,answer,rank_points,pay_points,user_money,user_rank,".
			" msn,qq,office_phone,home_phone,mobile_phone,passwd_question,passwd_answer ".
			"FROM ".$GLOBALS['ecs']->table('users'). " WHERE user_id = '$user_id'";
	$infos = $GLOBALS['db']->getRow($sql);
	$infos['user_name']= addslashes($infos['user_name']);
	
	$row = $user->get_profile_by_name($infos['user_name']);
	$_SESSION['email'] = $row['email'];
	
 	if($infos['user_rank']>0)
	{
		$sql = "SELECT rank_id,rank_name,discount From ".$GlOBALS['ecs']->table('user_rank').
				" WHERE rank_id = '$infos[user_rank]'"; 
	}
	else 
	{
		$sql ="SELECT rank_id,rank_name,discount,min_points".
				" FROM ".$GLOBALS['ecs']->table('user_rank') .
				" WHERE min_points<= " . intval($infos['rank_points']) . " ORDER BY min_points DESC";
	}
	
	if($row = $GLOBALS['db']->getRow($sql))
	{
		$info['rank_name'] = $row['rank_name'];
	}
	else
	{
		$info['rank_name'] = $GLOBALS['_LANG']['undifine_rank'];
	}
	
	$cur_date = date('Y-m-d H:i:s');
	
	$bonus = array();
	$sql = "SELECT type_name,type_money ".
			"FROM " . $GLOBALS['ecs']->table('bonus_type') . " AS t1, " . $GLOBALS['ecs']->table('user_bonus') . " AS t2 ".
			"WHERE t1.type_id = t2.bonus_type_id AND t2.user_id = '$user_id' AND t1.use_start_date <='$cur_date' " . 
			"AND t1.use_end_date > '$cur_date' AND t2.order_id =  0";
	
	$bonus = $GLOBALS['db']->getAll($sql);
	
	if($bonus)
	{
		for($i=0,$count=count($bonus); $i<$count; $i++)
		{
			$bonus[$i]['type_money'] = price_format($bonus[$i]['type_money'],false);
		}
	}
	
	$info['discount'] = $_SESSION['discount'] * 100 . "%";
	$info['email']	  = $_SESSION['email'];
	$info['user_name']= $_SESSION['user_name'];
	$info['rank_points']	= isset($infos['rank_points']) ? $infos['rank_points'] : '';
	$info['pay_points'] 	= isset($infos['pay_points']) ? $infos['pay_points'] : 0;
	$info['user_money']		= isset($infos['user_money']) ? $infos['user_money'] : 0;
	$info['sex']		= isset($infos['sex']) ? $infos['sex'] : '';
	$infos['birthday'] 	= isset($infos['birthday']) ? $infos['birthday'] : '';
	$infos['question'] 	= isset($infos['question']) ? htmlspecialchars($infos['question']) : '';
	
	$info['user_money'] = price_format($info['user_money'],false);
	$info['pay_points'] = $info['pay_points'] . $GLOBALS['_CFG']['integral_name'];
	$info['bonus']		= $bonus;
	$info['qq']			= $infos['qq'];
	$info['msn']		= $infos['msn'];
	$info['office_phone']	= $infos['office_phone'];
	$info['home_phone']		= $infos['home_phone'];
	$info['mobile_phone']	= $infos['mobile_phone'];
	$info['passwd_question']	= $infos['passwd_question'];
	$info['passwd_answer'] =$infos['passwd_answer'];
	
	return $info;
}